package com.gxa.modules.service.lyh.impl;
import com.gxa.common.vo.R2;
import com.gxa.modules.mapper.lyh.Customer_GoodsMapper;
import com.gxa.modules.pojo.Customers_Adders;
import com.gxa.modules.service.lyh.Customer_GoodsService;
import com.gxa.modules.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Transactional
@Service
public class Customer_GoodsServiceImpl implements Customer_GoodsService {
    R2 r=new R2();
    @Autowired(required = false)
    private Customer_GoodsMapper customer_goodsMapper;
    @Override
    public R2 goodsquery(Map map) {
        //如果是只有一个goods条件
        List<Map> result=customer_goodsMapper.goodsquery(map);
        List<Map> goodsTypeList=customer_goodsMapper.queryGoodsType();
        List list=new ArrayList();
        //添加访问的时间
        map.put("visitor_date", DateUtils.getCurrentTime("yyyy-MM-dd 00:00:00"));
        //商品id
        Object goods_id = map.get("goods_id");
        if (goods_id != null){
            Map map1 = result.get(0);
            System.out.println(map1);
            Object shop_id = map1.get("shop_id");
            map.put("shop_id",shop_id);
            //查询当前店铺访客最新日期是否为当日，不是当日就新增一条数据
            Map result1=customer_goodsMapper.queryVisitorDate(map);
            Object visitor_date = result1.get("visitor_date");
            System.out.println("查询出当前店铺的最新日期："+visitor_date);
            //为当日更新访客的数量 +1
            if (visitor_date!=null && !visitor_date.equals(' ')){
                //如果与最新的日期相等（跟新日期）,与最新的日期不相等而且不为空就插入一条新的数据
                if (visitor_date.equals(DateUtils.getCurrentTime("yyyy-MM-dd 00:00:00"))){
                    int result2=customer_goodsMapper.updateVisitor(map);
                }else {
                    int result2 = customer_goodsMapper.addVisitor(map);
                }
            }else {
                //为null表示该店铺第一次被访问新增一条访客数据
                int result2 = customer_goodsMapper.addVisitor(map);
            }
        }
        if (result!=null){
            list.add(result);
            list.add(goodsTypeList);
            r.setCode(200);
            r.setMsg("success!");
            r.setData(list);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 addCustomerOder(List<Map> map) {
        R2 r2=new R2();
        boolean falg=false;
            //1.添加订单并付款
        if(map!=null){
            for (Map p:map){
                if(p.get("status")!=null){
                 falg=true;
                }
            }
        }
        //如果flag==true表示为购物车添加的订单
        if (falg){
            //1.插入购物车的商品
            int result=customer_goodsMapper.addCustomerOder(map);
            //删除购物车的信息
            int result1=customer_goodsMapper.delShopingCar(map);

            if (result> 0 ){
                r2.setCode(200);
                r2.setMsg("success!");
            }else {
                r2.setCode(500);
                r2.setMsg("系统开小差!");
            }
        }else {
            //2.直接购买下单
            int result=customer_goodsMapper.addCustomerOder(map);
            if (result> 0 ){
                r2.setCode(200);
                r2.setMsg("success !");
            }else {
                r2.setCode(500);
                r2.setMsg("系统开小差!");
            }
        }
        //2.修改商品的库存
        for (Map p:map){
            int result2=customer_goodsMapper.updateGoods(p);
            //3.修改客户的累计消费金额
            int result3=customer_goodsMapper.updateConsumptionMoney(p);
            //4.查询消费者消费的金额
            Map map1 = customer_goodsMapper.queryConsumptionMoney(p);
            Double cumulative_charge =(Double) map1.get("cumulative_charge");
            p.put("cumulative_charge",cumulative_charge);
            //.判断是否满足会员升级条件，满足就更改
            int result4=customer_goodsMapper.updateVip(p);
        }
        return r2;
    }

    @Override
    public R2 queryShopingCar(Map map) {
        List<Map> result=customer_goodsMapper.queryShopingCar(map);
        if (result!=null){
            r.setCode(200);
            r.setMsg("success!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 addShopingCar(Map map) {
        int result=customer_goodsMapper.addShopingCar(map);
        if (result > 0){
            r.setCode(200);
            r.setMsg("添加商品成功!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 updateShopingCar(Map map) {
        int result=customer_goodsMapper.updateShopingCar(map);
        if (result > 0){
            r.setCode(200);
            r.setMsg("修改商品成功!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }
    @Override
    public R2 queryCustomerOder(Map map) {
        List<Map> result=customer_goodsMapper.queryCustomerOder(map);
        if (result!=null){
            r.setCode(200);
            r.setMsg("success!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 updateOrder(Map map) {
        int result=customer_goodsMapper.updateOder(map);
        if (result > 0){
            r.setCode(200);
            r.setMsg("success!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 addAdders(Map map) {
        Customers_Adders customers_adders=new Customers_Adders();
        //如果添加修改为默认地址，就将当前默认地址修改为备选地址
        if ((Integer)map.get("status")==1){
            int result1=customer_goodsMapper.updateStatusAdders(map);
        }
        //执行添加新地址
        int result=customer_goodsMapper.addAdders(map);
        long adders_id = customers_adders.getAdders_Id();
        if (result > 0){
            r.setCode(200);
            r.setMsg("success!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 queryAdders(Map map) {
        List<Map> result=customer_goodsMapper.queryAdders(map);
        if (result !=null){
            r.setCode(200);
            r.setMsg("success!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

    @Override
    public R2 updateAdders(Map map) {
        int result=customer_goodsMapper.updateOldAdders(map);
        int result1=customer_goodsMapper.updateNewAdders(map);
        if (result > 0 && result1 > 0 ){
            r.setCode(200);
            r.setMsg("修改成功!");
            r.setData(result);
        }else {
            r.setCode(500);
            r.setMsg("系统开小差!");
        }
        return r;
    }

}
